interface MyType {
    [property: string] : any
}
// 扁平数据转树形数据
export const arrayToTree=(items:any[])=>{
    const result = [];   // 存放结果集
    const itemMap:MyType = {};  // 将数组转化为map，暂时存放在这里
    for (const item of items) {
        const id = item.key;
        const pid = item.parent;

        if (!itemMap[id]) {
            itemMap[id] = {
                children: [],
            }
        }

        itemMap[id] = {
            ...item,
            children: itemMap[id]['children']
        }

        const treeItem =  itemMap[id];

        if (!pid) {
            result.push(treeItem);
        } else {
            if (!itemMap[pid]) {
                itemMap[pid] = {
                    children: [],
                }
            }
            itemMap[pid].children.push(treeItem)
        }

    }
    return result;
}

// 树形数据转扁平数据
export const treeToArray=(data:Array<any>)=>{
    let newArr=[];
    for(let i=0;i<data.length;i++){
        if(data[i].children){              // childrens存在
            console.log("有children存在")
            newArr.push(...treeToArray(data[i].children));
        }
        newArr.push({...data[i]});
    }
    console.log(newArr.length,"现在的长度")
    return newArr;
}
